package com.qa.controller.user;

import com.qa.dto.CategoryAddDTO;
import com.qa.dto.CategoryUpdateDTO;
import com.qa.entity.Category;
import com.qa.result.Result;
import com.qa.service.CategoryService;
import com.qa.utils.UserUtil;
import com.qa.vo.CategoryQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徐
 */
@Api(tags = "用户端分类相关接口")
@RestController
@RequestMapping("/user/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryAddDTO categoryAddDTO) {
        return categoryService.addCategory(categoryAddDTO);
    }

    @ApiOperation("查询该用户所有的分类")
    @GetMapping
    public Result<List<Category>> getAllCategory() {
        Long userId = UserUtil.getCurrentUserId();
        List<Category> category = categoryService.getCategoryByUser(userId);
        return Result.success(category);
    }

    @ApiOperation("根据分类名模糊查询别人的分类")
    @GetMapping("/name/{name}")
    public Result<List<CategoryQueryVO>> getCategoryByName(@PathVariable String name) {
        return categoryService.getCategoryByName(name);
    }

    @ApiOperation("修改自己的分类")
    @PutMapping
    public Result updateMyCategory(@RequestBody CategoryUpdateDTO categoryUpdateDTO) {
        return categoryService.updateMyCategory(categoryUpdateDTO);
    }

    @ApiOperation("删除自己的分类")
    @DeleteMapping("/{categoryId}")
    public Result deleteMyCategory(@PathVariable Long categoryId) {
        return categoryService.deleteMyCategory(categoryId);
    }
}
